/*******************************************************************************
 * Package: com.hadoop.mr
 * Type:    WordCountReduce
 * Date:    2024-05-14 22:35
 *
 * Copyright (c) 2024 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hadoop.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 功能描述： 聚合
 * Reducer1: <song,<1,1,1,1>>
 * Reducer2: <song,<1,1,1>>
 * Reducer3: <xian,<1,1>>
 * @author Songxianyang
 * @date 2024-05-14 22:35
 */
public class WordCountReduce extends Reducer<Text, IntWritable,Text, IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 定义变量
        int count = 0;
        // 循环map 给过来的这个<1,1,1,1>结构
        for (IntWritable value : values) {
            //个数
            count= value.get()+count;
        }
        // 写入上下文
        context.write(key,new IntWritable(count));

        System.out.println("---------Reduce--------");
        System.out.println(key+":Reduce:"+count);
    }
}
